package com.zhida.smartcontrol.dialog.dimmerdialog;

import android.app.Dialog;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.View;
import android.widget.SeekBar;

import com.zhida.smartcontrol.R;
import com.zhida.smartcontrol.common.Const;
import com.zhida.smartcontrol.common.bean.LampSwitchBean;
import com.zhida.smartcontrol.mvp.MVPBaseDialogFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * author: tanjianpei
 * date: 2019/12/13 013 10:57
 * description: 调光
 */
public class DimmerDialogFragment extends MVPBaseDialogFragment<DimmerDialogContract.View, DimmerDialogPresenter> implements DimmerDialogContract.View {
    @BindView(R.id.seek_bar)
    SeekBar mSeekBar;

    private String mType;
    private int mSwitchType;
    private List<LampSwitchBean> mLampList;

    public static DimmerDialogFragment newInstance(int switchType, List<LampSwitchBean> data) {
        Bundle args = new Bundle();
        args.putInt("type", switchType);
        args.putParcelableArrayList("list", (ArrayList<? extends Parcelable>) data);
        DimmerDialogFragment fragment = new DimmerDialogFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected Object getLayout() {
        return R.layout.dialog_dimmer;
    }

    @Override
    protected void initDatas(Bundle savedInstanceState) {
        Bundle bundle = getArguments();
        if (null == bundle) {
            return;
        }
        mType = "3";
        mSwitchType = bundle.getInt("type");
        mLampList = bundle.getParcelableArrayList("list");
    }

    @OnClick({R.id.btn_cancel, R.id.btn_confirm})
    public void click(View v) {
        switch (v.getId()) {
            case R.id.btn_cancel:
                dismiss();
                break;
            case R.id.btn_confirm:
                if (mSwitchType == Const.PLC_SINGLE || mSwitchType == Const.ZIG_BEE_SINGLE) {
                    mPresenter.plcBrightness(mLampList, mType);
                } else {
                    mPresenter.nbBrightness(mSwitchType, mLampList, mType);
                }
                break;
        }
    }

    @Override
    protected void initEvents(Dialog dialog) {
        mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                //1：100%         2：75%       3：50%       4：25%       0：0%
                if (progress < 25) {
                    progress = 0;
                    mSeekBar.setProgress(progress);
                    mType = "0";
                } else if (progress < 50) {
                    progress = 25;
                    mType = "4";
                    mSeekBar.setProgress(progress);
                } else if (progress < 75) {
                    progress = 50;
                    mType = "3";
                    mSeekBar.setProgress(progress);
                } else if (progress < 100) {
                    progress = 75;
                    mType = "2";
                    mSeekBar.setProgress(progress);
                } else {
                    progress = 100;
                    mType = "1";
                    mSeekBar.setProgress(progress);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }

    @Override
    public void onSuccess() {
        dismiss();
    }

}
